package alg20250308;


public class Text {
    public ListNode addTwoNumbers1(ListNode l1, ListNode l2) {
        ListNode newHead = new ListNode(0);
        ListNode tail = newHead;
        ListNode cur1 = l1, cur2 = l2;
        int t = 0;
        while(cur1 != null && cur2 != null){
            t += cur1.val + cur2.val;
            ListNode newNode = new ListNode(t % 10);
            tail.next = newNode;
            tail = newHead;
            t = t / 10;
            cur1 = cur1.next;
            cur2 = cur2.next;
        }

        while(cur1 != null){
            t += cur1.val;
            ListNode newNode = new ListNode(t % 10);
            tail.next = newNode;
            tail = newHead;
            t = t / 10;
            cur1 = cur1.next;
        }

        while(cur2 != null){
            t += cur2.val;
            ListNode newNode = new ListNode(t % 10);
            tail.next = newNode;
            tail = newHead;
            t = t / 10;
            cur2 = cur2.next;
        }

        if(t == 1){
            ListNode newNode = new ListNode(t);
            tail.next = newNode;
        }
        return newHead.next;
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode newHead = new ListNode(0);
        ListNode tail = newHead;
        int t = 0;
        ListNode cur1 = l1, cur2 = l2;
        while(cur1 != null || cur2 != null || t != 0){
            if(cur1 != null){
                t += cur1.val;
                cur1 = cur1.next;
            }

            if(cur2 != null){
                t += cur2.val;
                cur2 = cur2.next;
            }

            ListNode newNode = new ListNode(t % 10);
            tail.next = newNode;
            tail = newNode;
            t /= 10;
        }
        return newHead.next;
    }
}
